import axios from '@ohos/axios'
import { promptAction } from '@kit.ArkUI'
import type { AnyObject } from '../../models/HttpModel'

/**
 * node17
 * ohpm -v
 * ohpm install @ohos/axios
 */
const request = axios.create({
  baseURL: 'http://139.198.181.54:6060',
  timeout: 15000
})

request.interceptors.request.use(
  (config) => {
    // 将来会添加token参数
    return config
  }
)

request.interceptors.response.use(
  (response) => {
    if (response.data.code === 200) {
      return response.data.data
    } else {
      // 错误提示
      promptAction.showToast(response.data.message)
      return Promise.reject(response.data.message)
    }
  },
  (error) => {
    promptAction.showToast(error)
    return Promise.reject(error.message)
  })

export default class Http {
  get<T>(url: string, params?: AnyObject) {
    return request.get<any, T>(url, { params })
  }

  post<T>(url: string, data?: AnyObject) {
    return request.post<any, T>(url, data)
  }

  put<T>(url: string, data?: AnyObject) {
    return request.put<any, T>(url, data)
  }

  delete<T>(url: string, params?: AnyObject) {
    return request.delete<any, T>(url, { params })
  }
}